package com.brillilab.service.core.experiments.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.experiments.ExperimentBillMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.experiments.ExperimentBill;
import com.brillilab.service.core.experiments.IExperimentBillService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ExperimentBillServiceImpl implements IExperimentBillService {

    @Resource
    private ExperimentBillMapper experimentBillMapper;

    @Override
    public List<ExperimentBill> selectList(Long experimentId) {
        return experimentBillMapper.selectListWithRealAlias(experimentId);
    }

	@Override
	public boolean insertBatch(List<ExperimentBill> billList) {
        if(CollectionUtils.isNotEmpty(billList)){
            return experimentBillMapper.insertBatch(billList)>0;
        }
        return true;
	}

    @Override
    public ExperimentBill selectById(Long billId) {
        return experimentBillMapper.selectById(billId);
    }

    @Override
    public void updateById(ExperimentBill bill){
        int i=experimentBillMapper.updateById(bill);
        Assert.isTrue(i==1,"操作失败！");
    }

    @Override
    public List<ExperimentBill> selectList(List<Long> billIds) {
        if(billIds.size()>0){
            return experimentBillMapper.selectList(
                    new LambdaQueryWrapper<ExperimentBill>()
                            .in(ExperimentBill::getId,billIds));
        }
        return new ArrayList<>(0);
    }

    @Override
    public int updateBatch(List<ExperimentBill> billUpdate) {
        if(billUpdate.size()>0){
            return experimentBillMapper.updateBatch(billUpdate);
        }
        return 0;
    }

    @Override
    public int deleteById(Long id) {
        return experimentBillMapper.delete(
                new LambdaQueryWrapper<ExperimentBill>()
                        .eq(ExperimentBill::getId,id));
    }

    @Override
    public List<ExperimentBill> selectInStepReagentIds(List<Long> stepReagentIds) {
        if(stepReagentIds.size()>0){
            return experimentBillMapper.selectList(
                    new LambdaQueryWrapper<ExperimentBill>()
                            .in(ExperimentBill::getStepReagentId,stepReagentIds));
        }
        return new ArrayList<>(0);
    }

    @Override
    public List<ExperimentBill> selectList(Long experimentId,Long phaseId,Long stepId) {
        LambdaQueryWrapper<ExperimentBill> wrapper=new LambdaQueryWrapper<ExperimentBill>()
                .eq(ExperimentBill::getExperimentId,experimentId);
        if(phaseId!=null){
            wrapper.eq(ExperimentBill::getPhaseId,phaseId);

        }
        if(stepId!=null){
            wrapper.eq(ExperimentBill::getStepId,stepId);
        }
        return experimentBillMapper.selectList(wrapper);
    }

    @Override
    public List<ExperimentBill> selectByStepReagentId(Long stepReagentId) {
        return experimentBillMapper.selectList(
                new LambdaQueryWrapper<ExperimentBill>()
                        .eq(ExperimentBill::getStepReagentId,stepReagentId));
    }

	@Override
	public List<ExperimentBill> selectBillList(Long experimentId, Integer source, Long sourceId) {
		return experimentBillMapper.selectList(
                new LambdaQueryWrapper<ExperimentBill>()
                        .eq(ExperimentBill::getExperimentId,experimentId)
                        .eq(ExperimentBill::getSource,source)
                        .eq(ExperimentBill::getSourceId,sourceId));
	}
	
	@Override
	public List<ExperimentBill> selectBillList(Long experimentId, Integer source) {
		return experimentBillMapper.selectList(
				new LambdaQueryWrapper<ExperimentBill>()
				.eq(ExperimentBill::getExperimentId,experimentId)
				.eq(ExperimentBill::getSource,source));
	}

	@Override
	public int selectBillCount(Long experimentId) {
		return experimentBillMapper.selectCount(
				new LambdaQueryWrapper<ExperimentBill>()
				.eq(ExperimentBill::getExperimentId,experimentId));
	}

    @Override
    public List<ExperimentBill> selectByStepReagentIds(List<Long> ids) {
        if(ids!=null && ids.size()>0){
            return experimentBillMapper.selectList(new LambdaQueryWrapper<ExperimentBill>().in(ExperimentBill::getStepReagentId,ids));
        }
        return new ArrayList<>(0);
    }

    @Override
    public int deleteByIds(List<Long> billIds) {
        if(billIds!=null && billIds.size()>0){
            return experimentBillMapper.delete(
                    new LambdaQueryWrapper<ExperimentBill>()
                            .in(ExperimentBill::getId,billIds));
        }
        return 0;
    }

    @Override
    public List<ExperimentBill> selectBySourceAndExperimentId(Long sourceId,Integer source,Long experimentId) {
        return experimentBillMapper.selectList(
                new LambdaQueryWrapper<ExperimentBill>()
                        .eq(ExperimentBill::getExperimentId,experimentId)
                        .eq(ExperimentBill::getSource,source)
                        .eq(ExperimentBill::getSourceId,sourceId));
    }

    @Override
    public boolean deleteBySourceAndExperimentId(Long sourceId,Integer source,Long experimentId) {
        return experimentBillMapper.delete(
                new LambdaQueryWrapper<ExperimentBill>()
                        .eq(ExperimentBill::getExperimentId,experimentId)
                        .eq(ExperimentBill::getSource,source)
                        .eq(ExperimentBill::getSourceId,sourceId)) >=0 ;
    }

    @Override
    public List<ExperimentBill> getBillChartByDateRange(List<Long> experimentIdList,Date beginDate, Date endDate) {
        LambdaQueryWrapper<ExperimentBill> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ExperimentBill::getExperimentId, experimentIdList);
        queryWrapper.ge(ExperimentBill::getCreateTime, beginDate);
        queryWrapper.lt(ExperimentBill::getCreateTime, endDate);
        queryWrapper.eq(ExperimentBill::getSource,2);//来源：1-产物 2-试剂/样品成分
        return experimentBillMapper.selectList(queryWrapper);
    }

    @Override
	public void delete(Long experimentId) {
		LambdaQueryWrapper<ExperimentBill> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(ExperimentBill::getExperimentId, experimentId);
		wrapper.eq(ExperimentBill::getIsMethod, BoolEnum.FALSE.getValue());
		experimentBillMapper.delete(wrapper);
	}

	@Override
	public boolean insert(ExperimentBill bill) {
		return experimentBillMapper.insert(bill)>0;
	}

	@Override
	public void delete(Long experimentId, Integer source, List<Long> sourceIds) {
		LambdaQueryWrapper<ExperimentBill> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(ExperimentBill::getExperimentId, experimentId);
		wrapper.eq(ExperimentBill::getIsMethod, BoolEnum.FALSE.getValue());
		wrapper.eq(ExperimentBill::getSource, source);
		wrapper.in(ExperimentBill::getSourceId, sourceIds);
		experimentBillMapper.delete(wrapper);
	}

}
